package cn.stylefeng.guns.service.impl;

import cn.stylefeng.guns.dao.IGaiaProductBusinessService;
import cn.stylefeng.guns.dao.entity.GaiaBatchInfo;
import cn.stylefeng.guns.dao.entity.GaiaMaterialDoc;
import cn.stylefeng.guns.dao.entity.StatisticsSaleStockVO;
import cn.stylefeng.guns.dao.mapper.GaiaBatchInfoMapper;
import cn.stylefeng.guns.dao.mapper.GaiaMaterialDocMapper;
import cn.stylefeng.guns.dao.mapper.yaode.GaiaSdSaleDMapper;
import cn.stylefeng.guns.dao.mapper.yaode.GaiaSupplierBusinessMapper;
import cn.stylefeng.guns.dao.service.IGaiaSdSalePayMsgService;
import cn.stylefeng.guns.dao.service.IGaiaWmsDiaoboZService;
import cn.stylefeng.guns.modular.demo.controller.domain.StatisticsSaleStockRequest;
import cn.stylefeng.guns.modular.demo.service.SaleStockBizService;
import cn.stylefeng.guns.modular.demo.service.SyncService;
import cn.stylefeng.guns.util.RebateDateUtil;
import cn.stylefeng.roses.kernel.timer.api.TimerAction;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.jboss.logging.MDC;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.UUID;

/**
 * 数据同步任务
 */
@Component
@Slf4j
public class LogTimer implements TimerAction {


    @Resource
    private SaleStockBizService saleStockBizService;
    @Resource
    private GaiaMaterialDocMapper gaiaMaterialDocMapper;
    @Resource
    private GaiaBatchInfoMapper gaiaBatchInfoMapper;
    @Resource
    private IGaiaProductBusinessService gaiaProductBusinessService;

    @Resource
    private GaiaSupplierBusinessMapper gaiaSupplierBusinessMapper;
    @Resource
    private IGaiaSdSalePayMsgService gaiaSdSalePayMsgService;
    @Resource
    private GaiaSdSaleDMapper gaiaSdSaleDMapper;
    @Resource
    private IGaiaWmsDiaoboZService gaiaWmsDiaoboZService;

    @Override
    public void action(String params) {
        String traceId = UUID.randomUUID().toString();
        MDC.put("traceId", traceId);
        StatisticsSaleStockRequest statisticsSaleStockRequest = new StatisticsSaleStockRequest();
        statisticsSaleStockRequest.setStartTime(RebateDateUtil.getLastDayBegin(2));
        statisticsSaleStockRequest.setEndTime(RebateDateUtil.getLastDayEnd(2));
        StatisticsSaleStockVO statisticsSaleStockVO = saleStockBizService.getStatisticsSaleStockVOByTime(statisticsSaleStockRequest);
        log.info("统计销售库存数据:{}", JSON.toJSONString(statisticsSaleStockVO));
        QueryWrapper<GaiaMaterialDoc> queryWrapper = Wrappers.<GaiaMaterialDoc>query()
                .select("MAT_TYPE,count(1) matQty").groupBy("MAT_TYPE");
        List<GaiaMaterialDoc> gaiaMaterialDocs = gaiaMaterialDocMapper.selectList(queryWrapper);
        log.info("统计进销存数据药德:{}", JSON.toJSONString(gaiaMaterialDocs));

        Long l = gaiaBatchInfoMapper.selectCount(Wrappers.lambdaQuery());
        log.info("统计批次总数量:{}", l);

        log.info("统计商品总数量:{}", gaiaProductBusinessService.count(Wrappers.lambdaQuery()));
        log.info("统计供应商总数量:{}", gaiaSupplierBusinessMapper.selectCount(Wrappers.lambdaQuery()));
        log.info("统计支付数据总数量:{}", gaiaSdSalePayMsgService.count());
        log.info("统计销售商品总数量:{}", gaiaSdSaleDMapper.selectCount(Wrappers.lambdaQuery()));
        log.info("统计调拨总数量:{}", gaiaWmsDiaoboZService.count());
    }
}
